var app = getApp();
var findList = require('../data/find');

Page({
  data: {
    findList: findList.all,
    active: 'all',
    showLoadMore: false,
    isMore: true,
    idx: 0
  },
  onLoad: function (options) {
    // 页面加载时的逻辑
  },
  onReady: function () {
    // 页面初次渲染完成时的逻辑
  },
  onShow: function () {
    // 页面显示时的逻辑
  },
  onHide: function () {
    // 页面隐藏时的逻辑
  },
  onUnload: function () {
    // 页面卸载时的逻辑
  },
  showDetail: function (event) {
    // 点击商品列表项显示详情页的逻辑
    var id = event.currentTarget.dataset.idx;
    console.log('点击商品列表项，ID：', id);
  },
  chageFindData: function (event) {
    // 切换数据分类的逻辑
    console.log(event);
    var type = event.currentTarget.dataset.type;
    if (findList[type]) {
      this.setData({
        findList: findList[type],
        active: type,
        showLoadMore: false,
        idx: 0
      });
    }
  },
  scrollToLower: function (e) {
    // 滚动到底部加载更多的逻辑
    console.log(e, 123);
    var isMore = true;
    if (this.data.idx >= 1) {
      isMore = false;
    }
    this.setData({
      showLoadMore: true,
      isMore: isMore,
      idx: this.data.idx + 1
    });
  },
  navigateToDetail: function (event) {
    // 点击商品列表项跳转到详情页的逻辑
    var href = event.currentTarget.dataset.href;
    console.log('点击跳转，目标链接：', href);

    if (!href) {
      console.error('目标链接为空');
      return;
    }

    wx.navigateTo({
    //   url: '../detail/product_detail?url=' + encodeURIComponent(href),
    url: '../detail/product_detail?url=' + href,
    success: function (res) {
        console.log('跳转成功', res);
      },
      fail: function (err) {
        console.error('跳转失败', err);
      }
    });
  }
});
